package pck_tramitedocu.pck_src.pck_utiles;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Cls_Seguridad_DESBase64 {

    Cipher encrypt;
    Cipher decrypt;
    SecretKey key;
    String keyTxt;

    public Cls_Seguridad_DESBase64() {
        String clave = "4d89g13j4j91j27c582ji69373y788r6";
        this.keyTxt = clave;
        try {
            this.encrypt = Cipher.getInstance("DES");
            this.decrypt = Cipher.getInstance("DES");
            if (this.keyTxt.equals("")) {
                SecretKey key = KeyGenerator.getInstance("DES").generateKey();
                this.encrypt.init(1, key);
                this.decrypt.init(2, key);
            } else {
                KeySpec ks = new DESKeySpec(this.keyTxt.getBytes("UTF8"));
                SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
                SecretKey ky = kf.generateSecret(ks);
                this.encrypt.init(1, ky);
                this.decrypt.init(2, ky);
            }
        } catch (InvalidKeySpecException ex) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InvalidKeyException ex) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchPaddingException ex) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String encriptar(String str) {
        try {
            byte[] utf8 = str.getBytes("UTF8");
            byte[] enc = this.encrypt.doFinal(utf8);
            return new BASE64Encoder().encode(enc);
        } catch (BadPaddingException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        } catch (IllegalBlockSizeException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        } catch (UnsupportedEncodingException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        } catch (IOException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        }
        return null;
    }

    public String desencriptar(String str) {
        str = str.replace(" ", "+");
        try {
            byte[] dec = new BASE64Decoder().decodeBuffer(str);
            byte[] utf8 = this.decrypt.doFinal(dec);
            return new String(utf8, "UTF8");
        } catch (BadPaddingException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        } catch (IllegalBlockSizeException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        } catch (UnsupportedEncodingException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        } catch (IOException e) {
            Logger.getLogger(Cls_Seguridad_DESBase64.class.getName()).log(Level.SEVERE, null, e);
        }
        return null;
    }
}